package com.baizhi.controller;

import com.baizhi.entity.Admin;
import com.baizhi.realm.MyRealm;
import com.baizhi.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/3.
 */
@Controller
@RequestMapping("/Admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    private Logger logger =LoggerFactory.getLogger(AdminController.class);

    @RequestMapping("/login.do")
    public String login(String enCode,HttpSession session, Admin admin, Map map){
        System.out.println(admin+"---------------");
        Admin admin1=adminService.login(admin);
        if(admin!=null){
            session.setAttribute("admin",admin);
            return "redirect:/main/main.jsp";
        }else{
            map.put("tishi","用户不存在");
            return "login";
        }
    }

    @RequestMapping("/out.do")
    public String out(HttpSession session){
        System.out.println("---------------");
        session.removeAttribute("admin");
        return "login";
    }


    @RequestMapping("/adminRegeist.do")
    public String adminRegeist(Admin admin){
        System.out.println(admin+"1111111111111");
        try {
            adminService.adminRegeist(admin);
            return "redirect:/login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            return "redirect:/login.jsp";
        }
    }

    @RequestMapping("/adminLogin.do")
    public String adminLogin(HttpSession session,Admin admin){
        UsernamePasswordToken token=new UsernamePasswordToken(admin.getAdminUsername(),admin.getAdminPassword(),admin.getSalt());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            Admin admin1= adminService.adminLogin(admin);

            if(subject.hasRole("admin")){
                System.out.println("拥有管理员的权限------");
            }


            session.setAttribute("admin",admin1);
            logger.info("登录成功");
            return "redirect:/main/main.jsp";
        }catch (IncorrectCredentialsException e){
            logger.info("密码错误");
            return "redirect:/login.jsp";
        }catch (UnknownAccountException e1){
            logger.info("账号不存在");
            return "redirect:/login.jsp";
        }
    }

    @RequestMapping("/adminLogout.do")
    public String adminLogout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login.jsp";
    }

}
